<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" />
      </el-form-item>
      <!-- <el-form-item label="生产日期" >
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.scrq" type="date"
          value-format="yyyy-MM-dd" placeholder="选择生产日期">
        </el-date-picker>
      </el-form-item>
      -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['Productionline_Pet:Pet:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['Productionline_Pet:Pet:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['Productionline_Pet:Pet:remove']">删除</el-button>
        <!-- <el-button type="warning" plain icon="el-icon-s-check" size="mini" :disabled="multiple" @click="handleCheck"
          v-hasPermi="['Productionline_Pet:Pet:check']">审核</el-button> -->
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_Pet:Pet:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="conductivityList" @selection-change="handleSelectionChange" max-height="550">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="100px" />

      <el-table-column label="产品名称" align="center" prop="productName" width="300px" >
        <template slot-scope="scope">
          <span>{{scope.row.productName||'- -' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="size">
        <template slot-scope="scope">
          <span>{{scope.row.size||'- -' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" align="center" prop="scrq">
        <template slot-scope="scope">
          <span>{{scope.row.scrq||'- -' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="校准人" align="center" prop="jyr">
        <template slot-scope="scope">
          <span>{{scope.row.jyr||'- -' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="shhr">
        <template slot-scope="scope">
          <span>{{scope.row.shhr||'- -' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="250px">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-view" @click="excelhandleView(scope.row)">导出</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_Pet:Pet:edit']">修改</el-button>
          <el-button type="warning" plain icon="el-icon-s-check" size="mini" @click="handleCheck(scope.row)"
            v-hasPermi="['Productionline_Pet:Pet:verify']" v-if="scope.row.status==0">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_Pet:Pet:remove']">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改PET瓶（2L以下）检验原始记录对话框 -->
    <el-dialog v-myFullscreen :title="title+'PET瓶（2L以下）检验原始记录'" :visible.sync="open" width="900" custom-class="myDialog"
      append-to-body fullscreen>


      <div class="mainTitle">PET瓶（2L以下）检验原始记录</div>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="24">
            <el-col :span="8">
              <el-form-item label="产品名称" prop="productName">
                <el-input v-model="form.productName" placeholder="请输入产品名称" :disabled="openDisabled" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="规格" prop="size">
                <el-input v-model="form.size" placeholder="请输入规格" :disabled="openDisabled" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="生产日期" prop="scrq">
                <el-date-picker :editable="false" clearable size="small" v-model="form.scrq" type="date"
                  value-format="yyyy-MM-dd" placeholder="选择生产日期" :disabled="openDisabled">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="center"></el-divider>
            <el-table :data="qualityLabCapacitySubData" :row-class-name="rowQualityLabCapacitySubIndex" border
              ref="qualityLabCapacitySub" maxHeight="580px" :span-method="objectSpanMethod">
              <el-table-column minWidth="180px" align="center" label="检验项目" prop="b1">

              </el-table-column>
              <el-table-column minWidth="180px" align="center" label="标准要求" prop="b2">
              </el-table-column>

              <el-table-column minWidth="180px" align="center" label="检测数据">
                <el-table-column minWidth="120px" align="center" label="1" prop="a1">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a1" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="2" prop="a2">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a2" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="3" prop="a3">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a3" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="4" prop="a4">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a4" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="5" prop="a5">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a5" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="6" prop="a6">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a6" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="7" prop="a7">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a7" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="8" prop="a8">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a8" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="9" prop="a9">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a9" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="10" prop="a10">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a10" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="11" prop="a11">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a11" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="12" prop="a12">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a12" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="13" prop="a13">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a13" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="14" prop="a14">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a14" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="15" prop="a15">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a15" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="16" prop="a16">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a16" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="17" prop="a17">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a17" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="2" prop="a18">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a18" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="18" prop="a19">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a19" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="19" prop="a20">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a20" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="20" prop="a21">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a21" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="21" prop="a22">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a22" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="22" prop="a23">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a23" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="23" prop="a24">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a24" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="24" prop="a25">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a25" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
                <el-table-column minWidth="120px" align="center" label="25" prop="a4">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.a4" placeholder="请输入" :disabled="openDisabled" />
                  </template>
                </el-table-column>
              </el-table-column>
              </el-table-column>
              <el-table-column minWidth="180px" align="center" label="平均容量偏差">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.b3" placeholder="请输入平均容量偏差" :disabled="openDisabled" />
                </template>
              </el-table-column>
              <el-table-column minWidth="180px" align="center" label="检验结果">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.b4" placeholder="请输入检验结果" :disabled="openDisabled" />
                </template>
              </el-table-column>

            </el-table>
          </el-col>
          <el-col :span="24">
            <el-col :span="12">
              <el-form-item label="校准人" prop="jyr">
                <el-input v-model="form.jyr" placeholder="请输入校准人" :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="审核人" prop="shhr">
                <el-input v-model="form.shhr" placeholder="请输入审核人" :disabled="true" />
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listConductivity,
    getConductivity,
    delConductivity,
    addConductivity,
    updateConductivity,
    checkConductivity,
  } from "@/api/quality/productionline/pet";

  export default {
    name: "Conductivity",
    data() {
      return {
        //弹窗是否禁止编辑
        openDisabled: true,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedQualityLabCapacitySub: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // PET瓶（2L以下）检验原始记录表格数据
        conductivityList: [],
        // PET瓶（2L以下）检验原始记录子表格数据
        qualityLabCapacitySubData: [],
        // 弹出层产品名称
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 50,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
      };
    },
    created() {
      this.getList();
    },
    methods: {
      //合并行列
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (columnIndex === 0) {
          if (rowIndex === 4) {
            return {
              rowspan: 3,
              colspan: 1
            };
          } else if (rowIndex === 5 || rowIndex === 6) {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
          if (rowIndex === 7) {
            return {
              rowspan: 3,
              colspan: 1
            };
          } else if (rowIndex === 8 || rowIndex === 9) {
            return {
              rowspan: 0,
              colspan: 0
            }
          }
          // else if (rowIndex === 8) {
          //   return {
          //     rowspan: 3,
          //     colspan: 0
          //   };
          // } else {
          //   return {
          //     rowspan: 0,
          //     colspan: 0
          //   };
          // }
        }
      },
      /** 审核按钮操作 */
      handleCheck(row) {
        /* 审核 */
        checkConductivity(this.form)
          .then((response) => {
            this.msgSuccess("审核成功");
            this.getList();
          })
          .catch((error) => {
          });


      },
      /** 查询PET瓶（2L以下）检验原始记录列表 */
      getList() {
        this.loading = true;
        listConductivity(this.queryParams).then((response) => {
          this.conductivityList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          subData: [],
          shhr: '',
          xzr: ''
        }
        this.qualityLabCapacitySubData = []
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.openDisabled = false;
        this.open = true;
        this.title = "添加";
        this.form.jyr = this.$store.state.user.nickName;
        let qualityLabCapacitySubData = [
          { b1: '跌落性能', b2: '不破裂,可稳定地站立', b3: '', b4: '' },
          { b1: '瓶口', b2: '端面平整，螺纹应圆滑、无崩缺，无明显溢料毛边', b3: '', b4: '' },
          { b1: '瓶体', b2: '成型饱满，无气泡、无杂质、无变形，色泽均匀，瓶身无明显倾斜', b3: '', b4: '' },
          { b1: '瓶底', b2: '浇口不超过底平面，瓶能稳定地站立', b3: '', b4: '' },
          { b1: '高度偏差ΔH（mm）', b2: 'H＜150，偏差±1.0ΔH=H1-H0', b3: '', b4: '' },
          { b1: '高度偏差ΔH（mm）', b2: '150≤H≤300，偏差±1.5,ΔH=H1-H0', b3: '', b4: '' },
          { b1: '高度偏差ΔH（mm）', b2: 'H＞300，偏差±2.0ΔH=H1-H0', b3: '', b4: '' },
          { b1: '容量偏差(mL)', b2: 'V0＜500，偏差±3%，平均容量偏差≥0%，ΔV=V1-V0', b3: '', b4: '' },
          { b1: '容量偏差(mL)', b2: '500≤V0≤1000，偏差±15mL，平均容量偏差≥0ml，ΔV=V1-V0', b3: '', b4: '' },
          { b1: '容量偏差(mL)', b2: 'V0＞1000，偏差1.5%，平均容量偏差≥0%，ΔV=V1-V0', b3: '', b4: '' },
        ]
        for (let item of qualityLabCapacitySubData) {
          for (let i = 1; i < 26; i++) {
            item['a' + i] = ''
          }
        }
        this.qualityLabCapacitySubData = qualityLabCapacitySubData
        // this.form.operator = this.$store.state.user.nickName;
      },
      /** PET瓶（2L以下）检验原始记录子序号 */
      rowQualityLabCapacitySubIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** PET瓶（2L以下）检验原始记录子添加按钮操作 */
      handleAddQualityLabCapacitySub() {
        let obj = {};
        obj.a1 = "";
        obj.a2 = "";
        obj.a3 = "";
        this.qualityLabCapacitySubData.push(obj);
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.openDisabled = false;
        const id = row.id || this.ids;
        // this.queryParams.operator = null;
        getConductivity(id).then((response) => {
          this.form = response.data;
          this.qualityLabCapacitySubData = JSON.parse(this.form.subData) || []
          this.open = true;
          this.title = "修改";
          if (!this.form.jyr) {
            this.form.jyr = this.$store.state.user.nickName;
          }

        });
      },
      /** 审核按钮操作 */
      handleCheck(row) {
        this.reset();
        this.openDisabled = false;
        const id = row.id || this.ids;

        // this.queryParams.operator = null;
        getConductivity(id).then((response) => {
          this.form = response.data;
          this.qualityLabCapacitySubData = JSON.parse(this.form.subData) || []
          this.open = true;
          this.form.shhr = this.$store.state.user.nickName;
          this.title = "审核";
        });
      },
      /** 查看按钮 */
      handleView(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids;
        getConductivity(id).then((response) => {
          this.form = response.data;
          this.qualityLabCapacitySubData = JSON.parse(this.form.subData) || []
          this.open = true;
          this.title = "查看";
        });
      },

      excelhandleView(row) {
        this.download(
          "Productionline/conductivity/exportexcel?id=" + row.id,
          {
            ...this.queryParams,
          },
          "电导率仪校正记录" + this.parseTime(new Date()) + ".xlsx"
        );
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            // PET瓶（2L以下）检验原始记录子表格数据
            this.form.subData = JSON.stringify(this.qualityLabCapacitySubData)
            /**打开遮罩层*/
            const rLoading = this.openLoading();
            if (this.title == '修改') {
              updateConductivity(this.form)
                .then((response) => {
                  /**成功后执行关闭遮罩层*/
                  rLoading.close();
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                })
                .catch((error) => {
                  /**请求异常后执行关闭遮罩层*/
                  rLoading.close();
                });
            } else if (this.title == '添加') {
              addConductivity(this.form)
                .then((response) => {
                  /**成功后执行关闭遮罩层*/
                  rLoading.close();
                  this.msgSuccess("添加成功");
                  this.open = false;
                  this.getList();
                })
                .catch((error) => {
                  /**请求异常后执行关闭遮罩层*/
                  rLoading.close();
                });
            } else if (this.title == '审核') {
              this.form.status = 1
              checkConductivity(this.form)
                .then((response) => {
                  /**成功后执行关闭遮罩层*/
                  rLoading.close();
                  this.msgSuccess("审核成功");
                  this.open = false;
                  this.getList();
                })
                .catch((error) => {
                  /**请求异常后执行关闭遮罩层*/
                  rLoading.close();
                });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm(
          '是否确认删除数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delConductivity(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "Productionline_conductivity/conductivity/export",
          {
            ...this.queryParams,
          },
          `Productionline_conductivity_conductivity.xlsx`
        );
      },
      //接收编号
      nameChange(e) {
        this.form.bbbh = e;
      },
    },
  };
</script>
<style>
  .mainTitle {
    width: 100%;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 10px;
  }
</style>